# -*- coding: utf-8 -*-
"""
Created on Tue Jun  5 21:16:41 2018

@author: Administrator
"""
#这个脚本时利用T1像做完segment后得到的结果，进一步的得到各种阈值（[0.4,0.5,0.6,0.7,0.8,0.9]）的灰质mask。

#%%
#将T1_SC_SS文件夹中的c1subj_001_T1_SC_SS.nii等复制到T1_SC_SS_GM文件夹，并且重新命名
import os 
import shutil
import glob
source_dirpath = r'/media/root/Elements4/XuanWu_last/CBF_processing/T1_SC_SS'
target_dirpath = source_dirpath + '_GM'
if not os.path.exists(target_dirpath):
    os.makedirs(target_dirpath)
suffix = '.nii'
partial_str = 'c1'
partial_str1 = 'GM'
star = '*'
filename_list = glob.glob(source_dirpath + os.sep + partial_str + star )
for source_filepath in filename_list:
    print('source_filepath:',source_filepath)
    target_filename = os.path.split(source_filepath)[1].replace(partial_str,'').replace('.nii',r'_GM.nii')
    target_filepath = target_dirpath + os.sep + target_filename
    shutil.copy2(source_filepath,target_filepath)

#%%
from agilepipe.spine import AgilePipe

#the code below is used to process T1
modality = '_T1'     #'_T1' '_BOLD' '_CBF'  '_PET'
manipulate_methods = { 'Spm_normalize_SN':{},
                      'Spm_reslice_SR':{}
                      }
#dirpath 也是一个dict，其keys与manipulate_methods完全相同，但元素是对应处理方法所生成的文件夹的名字。
dirpath_dict = {'original_dirpath':[],
                     #用来存放其实的文件。
                     'Spm_normalize_SN':[],
                     'Spm_reslice_SR':[]
                    }

#agile_pipe()用ccs处理BOLD的测试代码
original_dirpath = r'/media/root/Elements4/XuanWu_last/CBF_processing/T1_SC_SS_GM'
#the first element of dirpath_list is very important which is used as deformation field 
#the second element of dirpath_list can be used in the reslice processing as the "Image Defining Space" filepath
dirpath_list = [r'/media/root/Elements4/XuanWu_last/CBF_processing/T1_SC_SS','/root/Documents/BrainMask_05_61x73x61.nii',''] #即使没有用也要赋值，否则会报错
dirpath_dict = dirpath_dict
manipulate_methods = manipulate_methods
parallel_core = 0.7
AgilePipe(modality,original_dirpath,dirpath_list,dirpath_dict,manipulate_methods,parallel_core)

#%% 下面是分别得到[0.4,0.5,0.6,0.7,0.8,0.9]各个阈值的mask
from agilepipe.process import Limit_mask
original_dirpath = r'/media/root/Elements4/XuanWu_last/CBF_processing/T1_SC_SS_GM_SN_SR'
limit = [0.4,0.5,0.6,0.7,0.8,0.9]
#limit = [0.4]
output_dirpath_list = []
for i in limit:
    output_dirpath_list.append(Limit_mask([original_dirpath,i]).output_dirpath)



#%%
from agilepipe.statics.basiccalculation import Multi_sum


group_filedirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask/T1_SC_SS_GM_SN_SR_LM-0.9'
output_dirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask'
Multi_sum([group_filedirpath,output_dirpath])


#%%

from agilepipe.statics.basiccalculation import  Multi_mean

group_dirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask/T1_SC_SS_GM_SN_SR_del-subj24'
output_dirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask'

Multi_mean([group_dirpath,output_dirpath])

#%%

from agilepipe.process.mask import  Greater_dot5_GD5


input_filepath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask/T1_SC_SS_GM_SN_SR_del-subj24_mean/T1_SC_SS_GM_SN_SR_del-subj24_mean.nii'
mask_filepath = r'/media/root/Elements4/XuanWu_last/mask/three_modality_groupmask/three_modality_groupmask.nii'
output_dirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask'

Greater_dot5_GD5([input_filepath,mask_filepath,output_dirpath])


#%%

from agilepipe.process.mask import Intersect_2mask

mask1_dirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask/T1_SC_SS_GM_SN_SR_LM-0.9_del-subj24'

mask2_dirpath = r'/media/root/Elements4/XuanWu_last/mask/GM_mask/T1_SC_SS_GM_SN_SR_del-subj24_mean_threshold-0.5'

Intersect_2mask([mask1_dirpath,mask2_dirpath])

#%%
